<?php
// Check to ensure this file is included in Joomla!
defined('_JEXEC') or die();

class ServersControllerServer extends ServersController
{
    /**
     * constructor (registers additional tasks to methods)
     */
    function __construct()
    {
        parent::__construct();

        // Register Extra tasks
        $this->registerTask('add', 'edit');
        $this->registerTask('unpublish', 'publish');
    }

    /**
     * display the edit form
     */
    function edit()
    {
        JRequest::setVar('view', 'server');
        JRequest::setVar('layout', 'form');
        JRequest::setVar('hidemainmenu', 1);

        parent::display();
    }

    /**
     * save a record (and redirect to  page)
     */
    function save()
    {
        $model = $this->getModel('server');

        if ($model->store($post)) {
            $msg = JText::_('Server config Saved!');
        } else {
            $msg = JText::_('Error Saving Server config');
        }

        $link = 'index.php?option=com_server';
        $this->setRedirect($link, $msg);
    }

    /**
     * remove record(s)
     */
    function remove()
    {
        $model = $this->getModel('server');
        if (!$model->delete()) {
            $msg = JText::_('Error: One or more Server config could not be Deleted');
        } else {
            $msg = JText::_('Server config Deleted');
        }

        $this->setRedirect('index.php?option=com_server', $msg);
    }


    /**
     * Publishes or Unpublishes one or more records
     */
    function publish()
    {
        $this->setRedirect('index.php?option=com_server');

        // Initialize variables
        $db =& JFactory::getDBO();
        $cid = JRequest::getVar('cid', array(), 'post', 'array');
        $task = JRequest::getCmd('task');
        $publish = ($task == 'publish');
        $n = count($cid);

        if (empty($cid)) {
            return JError::raiseWarning(500, JText::_('No items selected'));
        }

        JArrayHelper::toInteger($cid);
        $cids = implode(',', $cid);

        $query = 'UPDATE #__server'
            . ' SET published = ' . (int)$publish
            . ' WHERE id IN ( ' . $cids . ' )';

        $db->setQuery($query);
        if (!$db->query()) {
            return JError::raiseWarning(500, $this->db->getErrorMsg());
        }
        $this->setMessage(JText::sprintf($publish ? 'Items published' : 'Items unpublished', $n));

    }

    /**
     * cancel editing a record
     */
    function cancel()
    {
        $msg = JText::_('Operation Cancelled');
        $this->setRedirect('index.php?option=com_server', $msg);
    }
}

?>
